#include "widget.h"
#include "./ui_widget.h"

#include <QFileDialog>
#include <QProcess>
#include <QMessageBox>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

QString Widget::CreatSelectDilog()
{
    QFileDialog dialog;
    dialog.setFileMode(QFileDialog::Directory); // 设置选择模式为目录
    dialog.setWindowTitle("选择路径"); // 设置对话框标题

    // 显示对话框并获取用户选择的路径
    if (dialog.exec())
    {
        QStringList selectedPaths = dialog.selectedFiles();
        if (!selectedPaths.isEmpty())
        {
            QString selectedPath = selectedPaths.at(0);
            qDebug() << "选择的路径：" << selectedPath;
            return selectedPath;
        }
    }
    else{
        qDebug() << "路径选择失败" ;
        return " ";
    }
}


void Widget::on_btnMount_clicked()
{
    QString str1=ui->lineEdit_Upan->text();
    QString str2=ui->lineEdit_Mount->text();
    if(str1.isEmpty() || str2.isEmpty())
    {
        QMessageBox::information(nullptr, "失败", "输入框不能为空！");
        return;
    }

    str2 += ui->lineEdit_add->text();
    qDebug() << "str1 = " << str1.toStdString().c_str();
    qDebug() << "str2 = " << str2.toStdString().c_str();

    // 创建 QProcess 对象
    QProcess process;
    QString program = "./app";
    QStringList arguments;
    arguments << str1 << str2;
    // 启动进程并传递命令和参数
    process.start(program, arguments);

    // 等待进程完成
    process.waitForFinished();
    // 获取命令执行的输出结果
    QByteArray output = process.readAllStandardOutput();
    QByteArray error = process.readAllStandardError();

    // 打印输出和错误信息
    qDebug() << "Output:" << output;
    qDebug() << "Error:" << error;

    QMessageBox::information(nullptr, "成功", "操作成功完成！");
    //关闭窗口
    //this->close();
}

void Widget::on_pushButton_clicked()
{
    qDebug() << "on_pushButton_clicked = " ;

    QString processName = "app.exe";
    QString command1 = "tasklist";
    QProcess process;
    QStringList arguments1;
    arguments1 << "/FI" << QString("IMAGENAME eq %1").arg(processName);

    process.start(command1, arguments1);
    process.waitForFinished();
    QByteArray output = process.readAllStandardOutput();
    QString outputStr = QString::fromLocal8Bit(output);

    if (outputStr.contains(processName)) {
        qDebug() << processName << "正在运行";
    } else {
        QMessageBox::information(nullptr, "失败", "还没有挂载！");
        return;
    }


    QString program = "taskkill";
    QStringList arguments;
    arguments << "/F" << "/IM" << "app.exe";


    process.start(program, arguments);
    process.waitForFinished();
    // 获取命令执行的输出结果
    output = process.readAllStandardOutput();
    QByteArray error = process.readAllStandardError();

    // 打印输出和错误信息
    qDebug() << "Output:" << output;
    qDebug() << "Error:" << error;
    QMessageBox::information(nullptr, "成功", "操作成功完成！");
}

void Widget::on_btnSelect_1_clicked()
{
    QString str = CreatSelectDilog();
    ui->lineEdit_Upan->setText(str);
}


void Widget::on_btnSelect_2_clicked()
{
    QString str = CreatSelectDilog();
    ui->lineEdit_Mount->setText(str);
}

